#include <QtGui>

#include "polygonDialog.h"
#include "src/vector3d.h"

PolygonDialog::PolygonDialog(int _id, QWidget *_parent)
    : ObjectDialog(_id, _parent){
    setWindowTitle(QString::fromUtf8("Criando polígono ").append(QString::number(_id)));

    sides = 3;
    radius = 1;

    createLayout();
}

PolygonDialog::~PolygonDialog(){
}

void PolygonDialog::createLayout(){

    QSpinBox *sidesSpinBox = new QSpinBox();
    sidesSpinBox->setRange(3, 50);
    sidesSpinBox->setSingleStep(1);
    sidesSpinBox->setValue(sides);
    connect(sidesSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeSides(int)));

    QHBoxLayout *sidesLayout = new QHBoxLayout;
    sidesLayout->addWidget(sidesSpinBox);
    sidesLayout->addStretch(1);

    QGroupBox *sidesGroupBox = new QGroupBox(QString::fromUtf8("Lados"));
    sidesGroupBox->setLayout(sidesLayout);

    QDoubleSpinBox *radiusDoubleSpinBox = new QDoubleSpinBox();
    radiusDoubleSpinBox->setRange(0, 50.0);
    radiusDoubleSpinBox->setSingleStep(1.0);
    radiusDoubleSpinBox->setValue(radius);
    connect(radiusDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeRadius(double)));

    QHBoxLayout *radiusLayout = new QHBoxLayout;
    radiusLayout->addWidget(radiusDoubleSpinBox);
    radiusLayout->addStretch(1);

    QGroupBox *radiusGroupBox = new QGroupBox(QString::fromUtf8("Raio"));
    radiusGroupBox->setLayout(radiusLayout);

    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(sidesGroupBox);
    hLayout->addWidget(radiusGroupBox);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(hLayout);
    mainLayout->addLayout(createColorButtonLayout());
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void PolygonDialog::acceptAndCreate(){

    if(!object){
        object = new Object(id);
        object->makePolygon(sides, radius, Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
    }
    else if(colorChanged)
    {
        object->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
    }


    emit done(object, POLYGON);
    accept();
}

void PolygonDialog::changeSides(int _s){
    sides = _s;
}

void PolygonDialog::changeRadius(double _r){
    radius = _r;
}
